package com.qys.livesMall.common.message;

import com.qys.livesMall.common.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

/**
 * @menu MessageServer
 * @date 2023/05/27 16:00
 */
@Slf4j
@Component
public class MessageServer {

    @Autowired
    private GlobalLanguageConvertor languageConvertor;

    public R okMsg(QysLang qysLang,Object... args){
        String msg=languageConvertor.convertor(LocaleContextHolder.getLocale(),qysLang.getMsgKey(), qysLang.PROJECT());
        return R.ok(String.format(msg,args));
    }

    public R failMsg(QysLang qysLang,Object... args){
        String msg ="not find message";
        try {
            msg=languageConvertor.convertor(LocaleContextHolder.getLocale(),qysLang.getMsgKey(), qysLang.PROJECT());
        }catch (Exception e){
            log.error("未找到国际化信息："+e);
        }
        return R.fail(qysLang.getCode(),String.format(msg,args));
    }

    public String getMsg(QysLang qysLang,Object... args){
        String msg ="not find message";
        try {
            msg = languageConvertor.convertor(LocaleContextHolder.getLocale(), qysLang.getMsgKey(), qysLang.PROJECT());
        }catch (Exception e){
            log.error("未找到国际化信息："+e);
        }
        return String.format(msg,args);
    }


}

